package com.dhz.wlzs.app;

import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.undertow.UndertowBuilderCustomizer;
import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.DispatcherServlet;

import io.undertow.Undertow.Builder;

/**
 * war部署： 1、pom打包package改为war 2、Undertow依赖改为provided 3、更改启动类 public class Booter
 * extends SpringBootServletInitializer {
 * 
 * @Override protected SpringApplicationBuilder
 *           configure(SpringApplicationBuilder application) { return
 *           application.sources(Booter.class); }
 * @author heyiwu
 */
@SpringBootApplication
@EnableAsync
public class Booter {
	@Value("${spring.messages.basename}")
	private String basenames;
	@Value("${http.port}")
	private int port;

	public static void main(String[] args) {
		// springloaded
		// 1. mvn spring-boot:run
		// 2. vm args:
		// -javaagent:%m2_repo%\org\springframework\springloaded\1.2.7.RELEASE\springloaded-1.2.7.RELEASE.jar
		// -noverify
		// springboot devtools
		// System.setProperty("spring.devtools.restart.enabled", "false");
		// 设置dubbo日志适配
		// System.setProperty("dubbo.application.logger", "slf4j");
		// 关闭dubbo实时数据追踪
		// System.setProperty("dubbo.trace.enabled", "false");
		SpringApplication.run(Booter.class, args);
	}

	/**
	 * springmvc注册/初始化
	 * 
	 * @author: heyiwu
	 * @param dispatcherServlet
	 * @return
	 */
	@Bean
	public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
		ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
		dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
		registration.setLoadOnStartup(2);
		return registration;
	}

	@Bean
	public ResourceBundleMessageSource messageSource() throws Exception {
		ResourceBundleMessageSource rbms = new ResourceBundleMessageSource();
		rbms.setDefaultEncoding("UTF-8");
		rbms.setBasenames(basenames);
		return rbms;
	}

	@Bean
	public Validator validator() throws Exception {
		LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
		validator.setValidationMessageSource(messageSource());
		return validator;
	}

	@Bean
	public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
		UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory()  ;
		factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
			@Override
			public void customize(Builder builder) { // 多端口支持 //
				builder.addHttpListener(port, "0.0.0.0");
			}
 
		});
		return factory;
	}

}